//
//  DSSHTaskPhontoVcC.m
//  DSSH
//
//  Created by Fuhao on 16/4/18.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "DSSHTaskPhontoVcC.h"
#import "Photo.h"

@interface DSSHTaskPhontoVcC ()
{
    UIButton *_photoButotn;
    UIView   *_downView;
    Photo    *_photop;
    BOOL     _isPhoto;
    NSMutableArray *_imageArr;
}

@end

@implementation DSSHTaskPhontoVcC

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArr = [NSMutableArray array];
    [self MakeMyCapture];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    if (self.session) {
        
        [self.session startRunning];
    }
}


- (void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:YES];
    
    if (self.session) {
        
        [self.session stopRunning];
    }
}
#pragma mark 构造自定义相机
-(void)MakeMyCapture
{
    self.session = [[AVCaptureSession alloc] init];
    NSError *error;
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //更改这个设置的时候必须先锁定设备，修改完后再解锁，否则崩溃
    [device lockForConfiguration:nil];
    //设置闪光灯为自动
    [device setFlashMode:AVCaptureFlashModeAuto];
    [device unlockForConfiguration];
    
    self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
    self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
    [self.stillImageOutput setOutputSettings:outputSettings];
    
    if ([self.session canAddInput:self.videoInput]) {
        [self.session addInput:self.videoInput];
    }
    if ([self.session canAddOutput:self.stillImageOutput]) {
        [self.session addOutput:self.stillImageOutput];
    }
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.previewLayer.frame = CGRectMake(0, 64,[UIApplication sharedApplication].keyWindow.bounds.size.width , ZYAppHeight /6*4);
    self.view.layer.masksToBounds = YES;
    [self.view.layer addSublayer:self.previewLayer];
    
    //创建相机下面自定义视图
    [self createCusphototV];

}
-(void)createCusphototV
{
    CGFloat viewH = ZYAppHeight/6*2;
    _downView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.previewLayer.frame), ZYAppWidth, viewH)];
    _downView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_downView];
    //加上相机按钮和其他的自定义按钮
    UILabel *photoLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, ZYAppWidth-10, 30)];
   photoLable.text = @"拍照要求";
   photoLable.textColor = [UIColor whiteColor];
   [_downView addSubview:photoLable];
   _photoButotn = [[UIButton alloc] init];
    [_photoButotn setBackgroundImage:[UIImage imageNamed:@"xiangji"] forState:UIControlStateNormal];
    [_photoButotn addTarget:self action:@selector(SavePhoto:) forControlEvents:UIControlEventTouchUpInside];
   [_downView addSubview:_photoButotn];
   [_photoButotn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerX.equalTo(_downView.mas_centerX);
       make.top.equalTo(_downView.mas_top).with.offset(30);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(60);
    }];
    //创建状态button
    [self createstaButton:_downView];
}
-(void)createstaButton:(UIView *)Fview;
{
    CGFloat stabutotnW = (ZYAppWidth -80)/4;
    NSArray *arr = @[@"上刊",@"下刊",@"近景",@"远景"];
    for (int i =1 ; i < 5; i ++) {
        UIButton *staButton = [[UIButton alloc] init];
        [staButton setTitle:arr[i-1] forState:UIControlStateNormal];
        staButton.tintColor = [UIColor whiteColor];
        [staButton addTarget:self action:@selector(HitStabutton:) forControlEvents:UIControlEventTouchUpInside];
        staButton.tag = i+10;
        staButton.titleLabel.font = FONT(13);
        staButton.layer.cornerRadius = 5;
        staButton.layer.borderWidth = 1;
        staButton.layer.borderColor = [UIColor whiteColor].CGColor;
        [Fview addSubview:staButton];
        [staButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_photoButotn.mas_bottom).with.offset(20);
            make.height.equalTo(@30);
            make.width.mas_equalTo(stabutotnW);
            make.left.equalTo(Fview.mas_left).with.offset(10*i +(i-1)*(stabutotnW +10));
        }];
        
    }
    
}
-(void)SavePhoto:(UIButton *)button
{
    //进行拍照保存图片
    AVCaptureConnection *conntion = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
    if (!conntion) {
        NSLog(@"拍照失败!");
        return;
    }
    [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:conntion completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        if (imageDataSampleBuffer == nil) {
            return ;
        }
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        _photop = [[Photo alloc]init];
        _photop.image = [UIImage imageWithData:imageData];
        DSSHLog(@"拍照成功");
        [self Dophoto];
        
    }];
}
-(void)Dophoto {
    UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(300, 0, 60, 60)];
    [_downView addSubview:im];
    im.image = _photop.image;
}
-(void)HitStabutton:(UIButton *)button
{
    NSInteger myTag = button.tag;
    UIButton *other =nil;
    if(myTag ==11)
    {
        other = [_downView viewWithTag:12];
    }
    if(myTag ==12)
    {
        other = [_downView viewWithTag:11];
    }
    if(myTag ==13)
    {
        other = [_downView viewWithTag:14];
    }
    if(myTag ==14)
    {
        other = [_downView viewWithTag:13];
    }
    button.selected = !button.isSelected;
    other.selected = !button.isSelected;
    button.backgroundColor = button.isSelected ? [UIColor grayColor] :[UIColor blackColor];
    other.backgroundColor = other.isSelected ? [UIColor grayColor] :[UIColor blackColor];
    _isPhoto = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
